<?php

declare(strict_types=1);

namespace App\Chat\Controller;

use App\Chat\Annotation\ChatAuth;
use App\CustHub\Annotation\ChPermission;
use App\CustHub\Constants\MsgTypeConstants;
use App\CustHub\Mapper\ChCustomerServiceMapper;
use Hyperf\DbConnection\Db;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\PostMapping;

/**
 * 素材库
 */
#[Controller(prefix: 'ch/chat/material'),ChatAuth]
class MaterialController extends ChatBaseController
{

    #[Inject]
    protected ChCustomerServiceMapper $customerServiceMapper;

    #[PostMapping('list')]
    public function List()
    {
        $this -> validateParam([
            'is_personal' => 'required',
        ]);
        $uid =$this->request->input('uid');
        $customerService = $this->customerServiceMapper->findById($uid);

        $isPersonal =$this->request->input('is_personal');
        $page_size =$this->request->input('page_size',15);
        $params = $this -> request -> all();


        if($isPersonal == 1){
            $where = [
                ['m.uid','=',$customerService->uid],
                ['m.is_personal','=',1]
            ];
        }else{
            $where = [
                ['m.main_uid','=',$customerService->main_uid],
                ['m.is_personal','=',0]
            ];
        }
        if(!empty($params['keyword'])){
            $where[] = ['m.title','like',"%".$params['keyword']."%"];
        }
        if(!empty($params['msg_type'])){
            $where[] = ['m.msg_types','like',"%".$params['msg_type']."%"];
        }
        $query = Db::table('ch_material as m')
            -> leftJoin('ch_material_to_tag as tt','m.id','=','tt.material_id')
            -> leftJoin('ch_material_tag as t','t.id','=','tt.tag_id')
            -> select(['m.*',DB::raw('GROUP_CONCAT(t.id) as tag_ids'), DB::raw('GROUP_CONCAT(t.name) as tag_names')])
            -> groupBy('m.id')  // 根据 material 的 id 分组
            -> where($where);
        if(!empty($params['tag_id'])){
            $query -> havingRaw("FIND_IN_SET(?, tag_ids) > 0", [$params['tag_id']]);
        };
        $page = $query -> paginate((int) $page_size);
        foreach ($page as $item){
            $item ->  content =  !empty($item -> content)?json_decode($item -> content,true):[];
        }
        $list = $page -> items();
        $total = $page -> total();
        return $this -> success('获取成功',['list'=>$list,'total'=>$total]);
    }

    #[PostMapping('getMsgType')]
    public function getMsgType(): \Psr\Http\Message\ResponseInterface
    {
        $data = [
            [
                'value' => MsgTypeConstants::TEXT,
                'label' => MsgTypeConstants::getMessage(MsgTypeConstants::TEXT),
            ],
            [
                'value' => MsgTypeConstants::IMAGE,
                'label' => MsgTypeConstants::getMessage(MsgTypeConstants::IMAGE),
            ],
            [
                'value' => MsgTypeConstants::FILE,
                'label' => MsgTypeConstants::getMessage(MsgTypeConstants::FILE),
            ],
            [
                'value' => MsgTypeConstants::EMOJI,
                'label' => MsgTypeConstants::getMessage(MsgTypeConstants::EMOJI),
            ],
            [
                'value' => MsgTypeConstants::URL,
                'label' => MsgTypeConstants::getMessage(MsgTypeConstants::URL),
            ],
            [
                'value' => MsgTypeConstants::VIDEO,
                'label' => MsgTypeConstants::getMessage(MsgTypeConstants::VIDEO),
            ],
            [
                'value' => MsgTypeConstants::CUSTOM_MINI_PROGRAM,
                'label' => MsgTypeConstants::getMessage(MsgTypeConstants::CUSTOM_MINI_PROGRAM),
            ],
            [
                'value' => MsgTypeConstants::VIDEO_CHANNEL,
                'label' => MsgTypeConstants::getMessage(MsgTypeConstants::VIDEO_CHANNEL),
            ]
        ];

        return $this->success('获取成功',$data);
    }
}